package cz.cvut.fit.web.nakupniSeznam;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cz.cvut.fit.web.nakupniSeznam.dao.CategoryDAO;
import cz.cvut.fit.web.nakupniSeznam.dao.ItemDAO;
import cz.cvut.fit.web.nakupniSeznam.dao.ListDAO;
import cz.cvut.fit.web.nakupniSeznam.dao.UnitDAO;
import cz.cvut.fit.web.nakupniSeznam.dao.UserDAO;
import cz.cvut.fit.web.nakupniSeznam.data.Category;
import cz.cvut.fit.web.nakupniSeznam.data.Item;
import cz.cvut.fit.web.nakupniSeznam.data.List;
import cz.cvut.fit.web.nakupniSeznam.data.Unit;
import cz.cvut.fit.web.nakupniSeznam.data.User;

@SuppressWarnings("serial")
public class NakupniSeznamAndWebServlet extends HttpServlet {
	// public void doGet(HttpServletRequest req, HttpServletResponse resp)
	// throws IOException {
	// resp.setContentType("text/plain");
	// resp.getWriter().println("Hello, world");
	// }

	private static final Map<String, String> openIdProviders;
	static {
		openIdProviders = new HashMap<String, String>();
		openIdProviders.put("Google", "www.google.com/accounts/o8/id");
		openIdProviders.put("Yahoo", "yahoo.com");
		openIdProviders.put("MySpace", "myspace.com");
		openIdProviders.put("AOL", "aol.com");
		openIdProviders.put("MyOpenId.com", "myopenid.com");
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		initMock("128G7BKSGO");
		/*
		 * UserService userService = UserServiceFactory.getUserService(); //User
		 * user = userService.getCurrentUser(); // or req.getUserPrincipal()
		 * Set<String> attributes = new HashSet<String>();
		 * 
		 * resp.setContentType("text/html"); PrintWriter out = resp.getWriter();
		 * 
		 * if (user != null) { out.println("Hello <i>" + user.getNickname() +
		 * "</i>!"); out.println("[<a href=\"" +
		 * userService.createLogoutURL(req.getRequestURI()) +
		 * "\">sign out</a>]"); } else {
		 * out.println("Hello world! Sign in at: "); for (String providerName :
		 * openIdProviders.keySet()) { String providerUrl =
		 * openIdProviders.get(providerName); String loginUrl =
		 * userService.createLoginURL(req .getRequestURI(), null, providerUrl,
		 * attributes); out.println("[<a href=\"" + loginUrl + "\">" +
		 * providerName + "</a>] "); } }
		 */
	}

	public void initMock() {
		KeyGenerator gen = new KeyGenerator();
		
		User user = new User();
		user.setKey(gen.generateKey());

		Category c = new Category();
//		c.setMobileId((long) (Math.random() * 10));
		c.setName("kategorie "
				+ gen.generateKey());
		c.setOwner(user.getKey());
		c.setLastChanged(System.currentTimeMillis());

		Unit u = new Unit();
//		u.setMobileId((long) (Math.random() * 10));
		u.setName("jednotka "
				+ gen.generateKey());
		u.setOwner(user.getKey());
		u.setLastChanged(System.currentTimeMillis());

		List l = new List();
		l.setLastChanged(System.currentTimeMillis());
//		l.setMobileId((long) (Math.random() * 10));
		l.setName("list " + gen.generateKey());
		l.setOwner(user.getKey());

		Item i = new Item();
		i.setCreationDate(System.currentTimeMillis());
		i.setLastChanged(System.currentTimeMillis());
		i.setLastPicked(System.currentTimeMillis());
		i.setMarked(1);
//		i.setMobileId((long) (Math.random() * 10));
		i.setName("item " + gen.generateKey());
		i.setQuantity((int) (Math.random() * 10));

		java.util.List<Item> items = ItemDAO.getInstance().getAll();
		java.util.List<List> lists = ListDAO.getInstance().getAll();
		java.util.List<Category> categories = CategoryDAO.getInstance()
				.getAll();
		java.util.List<Unit> units = UnitDAO.getInstance().getAll();

		CategoryDAO.getInstance().save(c);
		UnitDAO.getInstance().save(u);
		UserDAO.getInstance().save(user);
		ListDAO.getInstance().save(l);
		ItemDAO.getInstance().save(i);

		for (Item item : items) {
			if (item.getListId() == null) {
				item.setListId(lists.get(lists.size()-1).getId());
			}

			if (item.getCategoryId() == null) {
				item.setCategoryId(categories.get(categories.size()-1).getId());
			}

			if (item.getUnitId() == null) {
				item.setUnitId(units.get(units.size()-1).getId());
			}
			ItemDAO.getInstance().save(item);
		}
		
		
	}
	
	public void initMock(String key) {

		Category c = new Category();
//		c.setMobileId((long) (Math.random() * 10));
		c.setName("kategorie "
				+ key);
		c.setOwner(key);
		c.setLastChanged(System.currentTimeMillis());

		Unit u = new Unit();
//		u.setMobileId((long) (Math.random() * 10));
		u.setName("jednotka "
				+ key);
		u.setOwner(key);
		u.setLastChanged(System.currentTimeMillis());

		List l = new List();
		l.setLastChanged(System.currentTimeMillis());
//		l.setMobileId((long) (Math.random() * 10));
		l.setName("list " +key);
		l.setOwner(key);

		Item i = new Item();
		i.setCreationDate(System.currentTimeMillis());
		i.setLastChanged(System.currentTimeMillis());
		i.setLastPicked(System.currentTimeMillis());
		i.setMarked(1);
//		i.setMobileId((long) (Math.random() * 10));
		i.setName("item " + key);
		i.setQuantity((int) (Math.random() * 10));

		java.util.List<Item> items = ItemDAO.getInstance().getAll();
		java.util.List<List> lists = ListDAO.getInstance().getAll();
		java.util.List<Category> categories = CategoryDAO.getInstance()
				.getAll();
		java.util.List<Unit> units = UnitDAO.getInstance().getAll();

		CategoryDAO.getInstance().save(c);
		UnitDAO.getInstance().save(u);
		ListDAO.getInstance().save(l);
		ItemDAO.getInstance().save(i);

		for (Item item : items) {
			if (item.getListId() == null) {
				item.setListId(lists.get(lists.size()-1).getId());
			}

			if (item.getCategoryId() == null) {
				item.setCategoryId(categories.get(categories.size()-1).getId());
			}

			if (item.getUnitId() == null) {
				item.setUnitId(units.get(units.size()-1).getId());
			}
			ItemDAO.getInstance().save(item);
		}
		
		
	}
}
